package com.kcsoft.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kcsoft.mapper.AdminMapper;
import com.kcsoft.pojo.Classes;
import com.kcsoft.pojo.Message;
import com.kcsoft.pojo.PageBean;
import com.kcsoft.pojo.User;
import com.kcsoft.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateUser(User user) {
        adminMapper.updates(user);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteUser(Integer id) {
        adminMapper.deleteUser(id);
    }

    @Override
    public void add(String username, String password, String stuId, Integer role, Integer classId) {
        adminMapper.add(username, password, stuId, role, classId);
    }

    @Override
    public void addModel(String name, String assistantId, Integer value, String description, String type) {
        adminMapper.addModel(name, assistantId, value, description, type);
    }

    @Override
    public void delModel(Integer id) {
        adminMapper.delModel(id);
    }

    @Override
    public PageBean<Classes> listClasses(Integer pageNum, Integer pageSize) {
        PageBean<Classes> pb = new PageBean<>();

        PageHelper.startPage(pageNum, pageSize);

        List<Classes> al = adminMapper.listClasses();

        Page<Classes> c = (Page<Classes>) al;

        pb.setTotal(c.getTotal());

        pb.setItems(c.getResult());

        return pb;
    }
}
